<?php
/**
 * @Author:         zk
 * @DateTime:       18-9-19 下午1:52
 * @Description:    冲洗 anhui 库中 maintenance_supplier 供应商的“logistics_freight-物流运费”数据     全国
 */
$api_service_path = __DIR__ . "/../../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
set_time_limit(0);
DB::connection()->disableQueryLog();      //关闭日志

/*
 * 供应商等级   全国级
 * logistics_freight		物流运费(1:含运费;2:不含运费;3:按区域设置;4:按门店设置;)
 * 文件格式 [中间分隔符为 #]    文件编码 utf-8
 * 供应商ID#供应商名称#物流运费(1:含运费;2:不含运费;)
 * 例如：
 * 1571#北京人和众达汽车用品销售#1
 * 1606#北京顺利汽车配件修理厂镗缸磨轴机加工服务中心（取螺#1
 */
//本地环境
//$txt_name = '\supplier_txt\logistics_freight_national.txt';     //供应商物流运费   anhui\api\public\supplier_txt\logistics_freight_national.txt
$txt_name = '/supplier_txt/logistics_freight_national.txt';     //供应商物流运费   anhui\api\public\supplier_txt\logistics_freight_national.txt
$path_name = public_path().''.$txt_name;
$content = file_get_contents($path_name);
ini_set('memory_limit', '-1');//不要限制Mem大小
$content_arr = explode("\n",$content);
$now_time = date('Y-m-d H:i:s', time());

$error_arr = [];    //有问题的数据数组
$supplier_id_str = '';  //供应商ID字符串
$sql = "UPDATE maintenance_supplier SET updated_at='".$now_time."', logistics_freight = CASE ";
foreach ($content_arr as $k => $v) {
    $line_arr = explode('#', $v);
    //判断数组的长度
    if(count($line_arr) == 3){
        $supplier_id = $line_arr[0];
        $supplier_id_str .= $supplier_id.',';
        
        $sql .= 'WHEN id='.$supplier_id.' THEN '.$line_arr[2].' ';
    }else{
        $error_arr[] = $v;  //不符合的数组
    }
}
$sql .= 'ELSE logistics_freight END WHERE id IN ('. rtrim($supplier_id_str, ',').');';

$res = DB::update(DB::raw($sql));
if($res){
    Log::info("供应商物流运费 ok 执行的数据条数为res: ".$res." "); 
    echo "供应商物流运费 ok 执行的数据条数为res: ".$res." \r\n";
} else {
    Log::info("供应商物流运费 error 执行的数据条数为res: ".$res." "); 
    echo "供应商物流运费 error 执行的数据条数为res: ".$res." \r\n";
}

//记录 异常的信息
if(!empty($error_arr)){
    $error_num = count($error_arr);
    $json_str = json_encode($error_arr);
    Log::info("供应商物流运费 异常的数据数量 num:".$error_num.",异常的数据为 ".$json_str." "); 
    echo "供应商物流运费 异常的数据数量 num:".$error_num.",异常的数据为 ".$json_str." \r\n";
}